//
//  WelcomeBackViewController.swift
//  Weibo10
//
//  Created by Leon on 15/10/18.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeBackViewController: UIViewController {
    
    /**
        设置根视图
    */
    override func loadView() {
        super.loadView()
        
        // 直接把背景图片设置为 view ,可以免设置frame 还可以自动变化大小
        view = backImageView
        
        setupUI()
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置头像
        placeholderImage.sd_setImageWithURL(UserAccountViewModel.shareAccount.avatarURL)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        welcomeLabel.hidden = true
        placeholderImage.transform = CGAffineTransformMakeTranslation(0, 200)
        
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                
                self.placeholderImage.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    
                    self.welcomeLabel.hidden = false
                    
                }) { (_) -> Void in
                    
                    // 接收通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchViewControllerNotification, object: nil)
                }
        }
    }

    /// 欢迎界面背景
    private lazy var backImageView: UIImageView = UIImageView(imageName:"ad_background")
    
    /// 欢迎头像
    private lazy var placeholderImage: UIImageView = {
       
        let iv = UIImageView(imageName: "avatar_default_big")
        // 切角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    /// 欢迎标语
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 18)
}

// MARK: - 设置界面
extension WelcomeBackViewController {
    
    private func setupUI() {
        
        // 添加子视图
        view.addSubview(placeholderImage)
        view.addSubview(welcomeLabel)
        
        // 设置约束
        placeholderImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_centerY).offset(-200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(placeholderImage.snp_centerX)
            make.top.equalTo(placeholderImage.snp_bottom).offset(16)
        }
        
        
        
        
    }
}




